import 'package:flutter/material.dart';
import 'package:flutter_widgets/flutter_widgets.dart';

class TagSample extends StatefulWidget {
  static const routeName = 'tag_sample';

  const TagSample({Key? key}) : super(key: key);

  @override
  State<TagSample> createState() => _TagSampleState();
}

class _TagSampleState extends State<TagSample> {
  List<String> selectTags = [];
  List<String> tags = [
    'Breathwork',
    'Self-Control',
    'Visualization',
    'Mental efficiency',
    'Neurofeedback',
    'Working Memory',
    'Pattern Matching',
    'Focused Attention',
    'Mental Flexibility',
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Tag Sample'),
      ),
      body: Stack(
        children: [
          Container(
            width: double.infinity,
            height: double.infinity,
            decoration: const BoxDecoration(
              gradient: LinearGradient(
                colors: [
                  Color(0xFF434B5F),
                  Color(0xFF222736),
                ],
              ),
            ),
            child: _content(),
          ),
        ],
      ),
    );
  }

  Widget _content() {
    return Container(
        padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 20),
        child: SingleChildScrollView(
          child: Column(
            children: [
              TagList(
                tagCount: tags.length,
                runSpacing: 8,
                spacing: 8,
                itemBuilder: (index) => _tag(index),
              ),
              TagList(
                  tagCount: tags.length,
                  runSpacing: 8,
                  spacing: 8,
                  itemBuilder: (index) {
                    var tag = tags[index];
                    var active = selectTags.contains(tag);
                    return tagItem(tag, active: active, onTap: () {
                      selectTag(tag);
                    });
                  }),
              OutlinedButton(
                onPressed: () {
                  setState(() {
                    selectTags.clear();
                  });
                },
                child: const Text('Rest'),
              ),
            ],
          ),
        ));
  }

  Widget _tag(int index) {
    return TagItem(
      tag: tags[index],
      leadingIcon: const Padding(
        padding: EdgeInsets.only(right: 9),
        child: Icon(
          Icons.message,
          size: 14,
          color: Color(0xFF9EECE9),
        ),
      ),
      //可以放删除按钮
      traillingIcon: index % 2 == 0
          ? GestureDetector(
              onTap: () {
                debugPrint('delete tag:$index');
              },
              child: Container(
                width: 20,
                height: 20,
                margin: const EdgeInsets.only(left: 12),
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10),
                    color: Colors.white),
                // alignment: Alignment.center,
                child: const Icon(
                  Icons.close,
                  size: 12,
                ),
              ),
            )
          : null,
      padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 5),
      color: const Color(0xFF9EDCE9).withOpacity(0.25),
      textStyle: const TextStyle(fontSize: 14, color: Color(0xFF9EECE9)),
      height: 36,
      border: Border.all(width: 0),
      onPressed: (tag) {
        debugPrint('click tag is $tag');
      },
    );
  }

  Widget tagItem(String tag,
      {bool active = false,
      bool canRemove = false,
      VoidCallback? onTap,
      Function(String tag)? onRemove}) {
    return TagItem(
      height: 40,
      canActive: true,
      tag: tag,
      padding: const EdgeInsets.symmetric(horizontal: 16),
      active: active,
      color: Colors.transparent,
      textStyle: const TextStyle(fontSize: 14, color: Color(0xFF9EECE9)),
      borderRadius: 32,
      border: Border.all(
        width: 1,
        color: Colors.red,
      ),
      onPressed: (tag) {
        onTap?.call();
      },
    );
  }

  void selectTag(String tag) {
    if (!selectTags.contains(tag)) {
      selectTags.add(tag);
    } else {
      selectTags.remove(tag);
    }
    setState(() {});
  }
}
